package com.hzdl.library.web.servlet;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.hzdl.library.domain.BookInfo;
import com.hzdl.library.domain.RecordPageBean;
import com.hzdl.library.domain.ResultInfo;
import com.hzdl.library.service.LibraryBookService;
import com.hzdl.library.service.impl.LibraryBookServiceImpl;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.beanutils.locale.converters.DateLocaleConverter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.net.URLDecoder;
import java.util.Date;
import java.util.List;
import java.util.Map;



//孙彬棋
@WebServlet("/book/*")
public class BookServlet extends BaseServlet {

    LibraryBookService libraryBookService = new LibraryBookServiceImpl();
    ObjectMapper objectMapper = new ObjectMapper();
//      添加书籍
    public void addBook(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ConvertUtils.register(new DateLocaleConverter(), Date.class);
        ResultInfo resultInfo = new ResultInfo();
        Map<String, String[]> map = req.getParameterMap();
        BookInfo bookInfo = new BookInfo();
        try {
            BeanUtils.populate(bookInfo,map);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        libraryBookService.addBook(bookInfo);
        resultInfo.setFlag(true);
        objectMapper.writeValue(resp.getOutputStream(),resultInfo);
    }
//      删除书籍
    public void deleteBook(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String bookId = req.getParameter("bookId");
        libraryBookService.deleteBook(bookId);
        resp.sendRedirect(req.getContextPath()+"/admin_books.html");
    }
//      修改书籍信息
    public void updateBook(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ConvertUtils.register(new DateLocaleConverter(), Date.class);
        ResultInfo resultInfo = new ResultInfo();
//        String bookId = req.getParameter("bookId");
        Map<String, String[]> map = req.getParameterMap();
        BookInfo bookInfo = new BookInfo();
        try {
            BeanUtils.populate(bookInfo,map);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        libraryBookService.updateBook(bookInfo);
        resultInfo.setFlag(true);
        objectMapper.writeValue(resp.getOutputStream(),resultInfo);
    }
//      查询书籍
    public void findAllBook(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        ResultInfo resultInfo = new ResultInfo();
        String book_id = req.getParameter("bookId");
        String rows = req.getParameter("rows");
        String currentPage = req.getParameter("currentPage");
        String searchBook = req.getParameter("searchBook");
        searchBook = URLDecoder.decode(searchBook, "utf-8");
        RecordPageBean<BookInfo> pageBean = libraryBookService.findAllBook(book_id,rows,currentPage,searchBook);
        resultInfo.setFlag(true);
        resultInfo.setData(pageBean);
        objectMapper.writeValue(resp.getOutputStream(),resultInfo);
    }
//    书籍详情
    public void findDetail(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String book_id = req.getParameter("bookId");
        BookInfo bookInfo = libraryBookService.findDetail(book_id);
        objectMapper.writeValue(resp.getOutputStream(),bookInfo);
    }
}
